package LeetCode._03_链表;

public class _04相交链表 {
    public class ListNode {
       int val;
       ListNode next;
       ListNode(int x) {
           val = x;
           next = null;
       }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) return null;
        int count = 0;
        ListNode t1 = headA;
        ListNode t2 = headB;
        while(t1 !=null){
            count++;
            t1 = t1.next;
        }
        while(t2!=null){
            count--;
            t2 = t2.next;
        }

        t1 = headA;
        t2 = headB;
        if(count > 0){
            while(count!=0){
                count--;
                t1 = t1.next;
            }

        }else if(count < 0){
            while(count!=0){
                count++;
                t2 = t2.next;
            }
        }

        while(t1!=t2){
            t1 = t1.next;
            t2 = t2.next;
        }
        return t1;
    }
}
